/* **************************************************************************/
/*COPYRIGHT CCWIN-IN ALL RIGHTS RESERVED. THIS IS AN UNPUBLISHED WORK.      */
/*V8:ConvertMode=Report                                                     */
/*V8:WebEnabled=Yes                                                         */
/*V8:RunMode=Character,Windows                                              */
/*EXTRACTING DATA FROM MFG/PRO                                              */
/****************************************************************************/
/* PROCEDURE NAME        : xxitemdapi.p                                     */
/* PROCEDURE TYPE        : JSON API Process                                 */
/* DESCRIPTION           : Item Master Load CIM-LOAD Process                */
/*                                                                          */     
/* INCLUDE FILES         :                                                  */
/* CALLED BY             :                                                  */
/* CALLED PROCEDURES     :                                                  */
/****************************************************************************/
/* CREATED BY            : Gaoyan                                6-Mar-24   */
/****************************************************************************/ 
/****************************************************************************/
/****************************************************************************/

{us/mf/mfdtitle.i}
{us/bbi/gplabel.i}
{us/wb/wbrp01.i}
{xxjson.i}
{xxcimload.i}

{xxapilog.i}

&SCOPED-DEFINE TimeStamp1 substring(string(year(today), "9999"),3) + string(month(today), "99") + string(day(today), "99")
&SCOPED-DEFINE TimeStamp2 substring(string(time, "HH:MM:SS"), 1, 2) + substring(string(time, "HH:MM:SS"), 4, 2) + substring(string(time, "HH:MM:SS"), 7, 2)

define input parameter vc_inputjson             as character format "x(60)" no-undo.
define input parameter vc_outputjson            as character format "x(60)" no-undo.
define input parameter vc_import                as logical  no-undo.


DEFINE VARIABLE li_access           AS    INTEGER                       NO-UNDO.
define variable l_file                       as character format "x(40)" no-undo.
define variable l_line                       as character   no-undo.
define variable lvi_row_id                   as decimal     no-undo.
define variable v_import                     as logical     init   "no"      no-undo.
define variable v_site                       like si_site   no-undo.

define variable l_error                      as character      no-undo.
define stream f_log.
define stream f_cim.
define variable v_quote               as character initial """"   no-undo.
define variable v_default             as character initial "-"    no-undo.
define variable v_space               as character initial " "    no-undo.
define variable v_log                 as character                no-undo.
define variable v_cim                 as character                no-undo.
define variable v_fline               as character                no-undo.
define variable v_errorstatus         as logical                  no-undo.
define variable v_toterrorstatus         as logical                  no-undo.
define variable v_errstring           as character                no-undo.
define variable v_status              as character                no-undo.

define variable v_entity              like si_entity              no-undo.
define variable v_ee                  as logic                    no-undo.

define variable vi_filecount          as integer                  no-undo.
define variable vi_reccount           as integer                  no-undo.
define variable vc_filename           as character                no-undo.
define variable vc_topath             as character                no-undo.
define variable v_runproagram        as character                no-undo.
define variable vc_traceid            as character                no-undo.


            
define temp-table ttptpdet no-undo
            field ptp_part                 like ptp_det.ptp_part
            field ptp_site                 like ptp_det.ptp_site 
            field ptp_ms                   like ptp_det.ptp_ms                
            field ptp_plan_ord             like ptp_det.ptp_plan_ord          
            field ptp_timefnce             like ptp_det.ptp_timefnce          
            field ptp_ord_pol              like ptp_det.ptp_ord_pol           
            field ptp_ord_qty              like ptp_det.ptp_ord_qty           
            field ptp_ord_per              like ptp_det.ptp_ord_per           
            field ptp_sfty_stk             like ptp_det.ptp_sfty_stk          
            field ptp_sfty_tme             like ptp_det.ptp_sfty_tme          
            field ptp_rop                  like ptp_det.ptp_rop               
            field ptp_rev                  like ptp_det.ptp_rev               
            field ptp_iss_pol              like ptp_det.ptp_iss_pol           
            field ptp_buyer                like ptp_det.ptp_buyer             
            field ptp_vend                 like ptp_det.ptp_vend              
            field ptp_po_site              like ptp_det.ptp_po_site           
            field ptp_pm_code              like ptp_det.ptp_pm_code           
            field ptp_gcfg_type            like ptp_det.ptp_gcfg_type         
            field ptp_insp_loc             like ptp_det.ptp_insp_loc          
            field ptp_ins_rqd              like ptp_det.ptp_ins_rqd           
            field ptp_ins_lead             like ptp_det.ptp_ins_lead          
            field ptp_cum_lead             like ptp_det.ptp_cum_lead          
            field ptp_mfg_lead             like ptp_det.ptp_mfg_lead          
            field ptp_pur_lead             like ptp_det.ptp_pur_lead          
            field ptp_atp_enforcement      like ptp_det.ptp_atp_enforcement   
            field ptp_atp_family           like ptp_det.ptp_atp_family                                              
            field ptp_atp_horizon          like ptp_det.ptp_atp_horizon       
            field ptp_run_seq1             like ptp_det.ptp_run_seq1          
            field ptp_run_seq2             like ptp_det.ptp_run_seq2          
            field ptp_Phantom              like ptp_det.ptp_Phantom           
            field ptp_ord_max              like ptp_det.ptp_ord_max           
            field ptp_ord_min              like ptp_det.ptp_ord_min           
            field ptp_ord_mult             like ptp_det.ptp_ord_mult                                            
            field ptp_op_yield             like ptp_det.ptp_op_yield          
            field ptp_yld_pct              like ptp_det.ptp_yld_pct           
            field ptp_run                  like ptp_det.ptp_run               
            field ptp_setup                like ptp_det.ptp_setup             
            field ptp_network              like ptp_det.ptp_network                                                
            field ptp_routing              like ptp_det.ptp_routing           
            field ptp_bom_code             like ptp_det.ptp_bom_code          
            field ptp_replenishment_mthd   like ptp_det.ptp_replenishment_mthd
            field ptp_traceid              as   character
            field ptp_logfile              as   character
            field ptp_cimfile              as   character
            field ptp_dataid              as   character.

      DEFINE DATASET dsPartsiteplandetail FOR ttptpdet.

      hdataset = dataset dsPartsiteplandetail:HANDLE.    


mainloop:
repeat on error undo mainloop, retry mainloop:

   /* Output and Batch Input */
   
   {us/xx/xxworkpath_e.i &outbound=YES &inbound=no &archive=YES}

assign m_outpath     = m_outpath + "pt/"
       m_archpath    = m_archpath + "pt/"
       m_successpath = m_successpath + "pt/"
       m_errorpath   = m_errorpath + "pt/"
       m_workpath    = m_workpath + "pt/".


   empty temp-table ttptpdet.
      v_runproagram = "".
     vc_traceid = "".
   /* Load item master data from JSON  */ 
   vc_outputjson = replace(vc_inputjson,".json",".run.out.json").   
   run loadjsonfile(input vc_inputjson,
                    input vc_outputjson,
                    output vl_RetOK).
   if vl_RetOK then
   do:
      find first ttptpdet no-error.
      assign vc_traceid = ttptpdet.ptp_traceid.
   end.
   
  /* Create CIM-LOAD file and do cim-load */
  v_cim = string(vc_traceid).
  v_log = string(vc_traceid).
  
 output to /home/mfg/work/cim/xxitemplanapi.log  .      
 
if vc_import and vl_RetOK then do:  

  /* create cim load format */
                
  cimloadprocess:
  do transaction on error undo cimloadprocess, leave cimloadprocess: 
  
  for each ttptpdet  break by ttptpdet.ptp_part.

    v_cim = m_workpath + "PT" + "_" + string(ttptpdet.ptp_traceid) + "_" + string(ttptpdet.ptp_part) + "_" + {&TimeStamp1} + "_" + {&TimeStamp2}  + ".cim".
    v_log = m_workpath + "PT" + "_" + string(ttptpdet.ptp_traceid) + "_" + string(ttptpdet.ptp_part) + "_" + {&TimeStamp1} + "_" + {&TimeStamp2}  + ".log".
    output stream f_cim to value(v_cim).

	 put stream f_cim  unformatted 
             v_quote ttptpdet.ptp_part            v_quote v_space  /* Part Number */
             v_quote ttptpdet.ptp_site            v_quote skip     /*  Site  */			 
             v_quote ttptpdet.ptp_ms              v_quote v_space  /*  1.4.17 MasterSchedule */
             v_quote ttptpdet.ptp_plan_ord        v_quote v_space  /*  1.4.17 Plan Order */
             v_quote ttptpdet.ptp_timefnce        v_quote v_space  /*  1.4.17 Time Fence */
             v_quote ttptpdet.ptp_ord_pol         v_quote v_space  /*  1.4.17 Order Policy */
             v_quote ttptpdet.ptp_ord_qty         v_quote v_space  /*  1.4.17 Order Quantity */
             v_quote ttptpdet.ptp_ord_per         v_quote v_space  /*  1.4.17 Order Period */
             v_quote ttptpdet.ptp_sfty_stk        v_quote v_space  /*  1.4.17 Safety Stock */
             v_quote ttptpdet.ptp_sfty_tme        v_quote v_space  /*  1.4.17 Safety Time*/
             v_quote ttptpdet.ptp_rop             v_quote v_space  /*  1.4.17 Re-order Point */
             v_quote ttptpdet.ptp_rev             v_quote v_space  /*  1.4.17 Planning  Revision */
             v_quote ttptpdet.ptp_iss_pol         v_quote v_space  /*  1.4.17 Issue Policy */ 
             v_quote ttptpdet.ptp_buyer           v_quote v_space  /*  1.4.17 Buyer Planner */
             v_quote ttptpdet.ptp_vend            v_quote v_space  /*  1.4.17 Supplier */
             v_quote ttptpdet.ptp_po_site         v_quote v_space  /*  1.4.17 Po Site  */
             v_quote ttptpdet.ptp_pm_code         v_quote v_space  /*  1.4.17 Purchase Manufacture */
             v_quote ttptpdet.ptp_gcfg_type       v_quote v_space  /*  1.4.17 Configuration Type */
             v_quote ttptpdet.ptp_insp_loc        v_quote v_space  /*  1.4.17 Inspection Location */
             v_quote ttptpdet.ptp_ins_rqd         v_quote v_space  /*  1.4.17 Inspection Required */
             v_quote ttptpdet.ptp_ins_lead        v_quote v_space  /*  1.4.17 Inspection Lead Time */
             v_quote ttptpdet.ptp_cum_lead        v_quote v_space  /*  1.4.17 Cumulative Lead Time */
             v_quote ttptpdet.ptp_mfg_lead        v_quote v_space  /*  1.4.17 Manufacture Lead TIme */
             v_quote ttptpdet.ptp_pur_lead        v_quote v_space  /*  1.4.17 Purchase Lead Time */
             v_quote ttptpdet.ptp_atp_enforcement v_quote v_space  /*  1.4.17 Atp Enforecement Level */
             v_quote ttptpdet.ptp_atp_family      v_quote v_space  /*  1.4.17 Family ATP */
             v_quote ttptpdet.ptp_atp_horizon     v_quote v_space  /*  1.4.17 Family Horizon */
             v_quote ttptpdet.ptp_run_seq1        v_quote v_space  /*  1.4.17 Run Seq1 */
             v_quote ttptpdet.ptp_run_seq2        v_quote v_space  /*  1.4.17 Run Seq2 */
             v_quote ttptpdet.ptp_Phantom         v_quote v_space  /*  1.4.17 Phantom */
             v_quote ttptpdet.ptp_ord_max         v_quote v_space  /*  1.4.17 MINIMUM Order Quantity  */
             v_quote ttptpdet.ptp_ord_min         v_quote v_space  /*  1.4.17 Maximum Order Quantity */
             v_quote ttptpdet.ptp_ord_mult        v_quote v_space  /*  1.4.17 Order MULTIPLE */
             v_quote ttptpdet.ptp_op_yield         v_quote v_space /*  1.4.17 Operation Based Yield */
             v_quote ttptpdet.ptp_yld_pct          v_quote v_space /*  1.4.17 Yield Percent */
             v_quote ttptpdet.ptp_run              v_quote v_space /*  1.4.17 Run Time */
             v_quote ttptpdet.ptp_setup            v_quote v_space /*  1.4.17 Setup Time */
             v_default v_space                                     /*  1.4.17 EMT type */
             v_default v_space                                     /*  1.4.17 EMT Auto  */
             v_quote ttptpdet.ptp_network          v_quote v_space /*  1.4.17 Net work code */
             v_quote ttptpdet.ptp_routing          v_quote v_space /*  1.4.17 Routing Code */
             v_quote ttptpdet.ptp_bom_code         v_quote v_space /*  1.4.17 Bom Formula code */
             v_default v_space skip   /*  1.4.17 Replenishment method */
             .	                 


	

    output stream f_cim close.
    
         
     /* cim load process */
     v_runproagram = "pppsmt02.p".
     execname = "xxitemsiteplanapi.p".

     
       put unformatted v_cim " " skip
                       v_log " " skip 
                       m_outpath " " skip
                       m_archpath " " skip
                       v_runproagram " " skip
                       v_errstring " " skip 
                       v_errorstatus skip .
                       
       run cimloadprocess(input v_cim,
                          input v_log,
                          input v_runproagram,
                          input vc_traceid,
                          input ttptpdet.ptp_dataid,
                          input ttptpdet.ptp_part,
                          output v_errstring,
                          output v_errorstatus) .
                          
       v_toterrorstatus = v_toterrorstatus or v_errorstatus.    


            /* Handle cim and log file */
        do vi_filecount = 1 to 2:
          /* when success move cim log file to archive directory  */
          /* when failed move cim log file to error directory  */   
          /*  move status json file to out directory for API return */   
          /* when success move previous step .p and json file to archive directory  */
          /* when failed move previous step .p and json file to error directory  */       
          case vi_filecount:
          when 1 then
          assign vc_filename = v_cim
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath.
          when 2 then
          assign vc_filename = v_log
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath.
           end case.           
          run cimfilehandle(input vc_filename,
                            input vc_topath,
                            input no ).       

            assign   ttptpdet.ptp_cimfile = replace(v_cim,m_workpath,vc_topath)
                    ttptpdet.ptp_logfile = replace(v_log,m_workpath,vc_topath).                            
                 
       end. /* do vi_filecount = 1 to 2: */   
    
     
    end. /* for each ttptp  */

     if  v_toterrorstatus then 

       undo cimloadprocess, leave cimloadprocess.          
            
        
    end.    /* do transaction on error undo cimloadprocess, leave cimloadprocess: */

       if can-find(first api_status where api_status.return_status = "FAILED" ) then
       do:
       
       for each api_status where api_status.return_status = "SUCCESS".
         
         delete api_status.
         
       end.
       
       end.  /* if can-find(first api_status */
         


   end. /* if vc_import */
   
   
    for each ttptpdet break by ttptpdet.ptp_part .
       if last-of(ttptpdet.ptp_part) then 
       do:
       vi_reccount = vi_reccount + 1.
              
       run CreateAPILogMaster(input vc_traceid,
                              input "itemsiteLocdetail",
                              input "xxitemsitelocapi.p",
                              input ttptpdet.ptp_cimfile,
                              input ttptpdet.ptp_logfile,
                              input vc_inputjson,
                              input ttptpdet.ptp_part,
                              input "",
                              input "xxapiitemlocdata_det",
                              input vi_reccount).  
                              
       end. /*  if last-of(ttptpdet.ptp_part) then  */
        
      end. /*   for each ttptidet break by ttptpdet.ptp_part . */
        
   
        /* Handle cimload related files */
         /* Create output json */                   
         run writejsonfile(input hresult,
                  input vc_outputjson,
                  input "FILE",
                  input True,
                  input "UTF-8",
                  output vl_RetOK,
                  output vc_errorstring).
                  
         put unformatted v_cim " " skip
                         v_log " " skip 
                         m_outpath " " skip
                         m_archpath " " skip
                         v_runproagram " " skip
                         v_errstring " " skip 
                         v_errorstatus skip .
       do vi_filecount = 1 to 5:
          /* when success move cim log file to archive directory  */
          /* when failed move cim log file to error directory  */   
          /*  move status json file to out directory for API return */   
          /* when success move previous step .p and json file to archive directory  */
          /* when failed move previous step .p and json file to error directory  */       
          case vi_filecount:
      
          when 1 then
          assign vc_filename = replace(vc_inputjson,".json",".p")
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 2 then
          assign vc_filename = vc_inputjson
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 3 then
          assign vc_filename = replace(vc_inputjson,".json",".dat")
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 4 then
          assign vc_filename = replace(vc_inputjson,".json",".run.log")
                 vc_topath   = m_outpath.
          when 5 then
          assign vc_filename = replace(vc_inputjson,".json",".run.out.json")
                 vc_topath   =  m_outpath.
          end case.
          
         if vl_RetOK then 
         put unformatted vc_filename " " skip
         vc_topath " " skip 
         vi_filecount skip.
     
        
          
          run cimfilehandle(input vc_filename,
                            input vc_topath,
                            input no ).                 
                   
          
     
       end.       /* do vi_filecount = 1 to 5: */        
       
       output close.   
   
   
   leave.
end. /* repeat on error undo mainloop, retry mainloop: */
